package com.my.oschina.asynctask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.widget.Toast;

/**
 * LoadingDialog：抽象类，继承了AsyncTask类
 */
public abstract class LoadingDialogTask<Input, Entity> extends
		AsyncTask<Input, WSError, Entity> {

	private ProgressDialog mProgressDialog;
	protected Activity mActivity;
	private int mLoadingMsg;
	private int mFailMsg;
	private boolean cancelable = true;

	/**
	 * 构造方法。
	 * 
	 * @param activity
	 * @param loadingMsg
	 * @param failMsg
	 */
	public LoadingDialogTask(Activity activity, int loadingMsg, int failMsg) {
		this.mActivity = activity;
		this.mLoadingMsg = loadingMsg;
		this.mFailMsg = failMsg;
	}

	public boolean isCancelable() {
		return cancelable;
	}

	public void setCancelable(boolean cancelable) {
		this.cancelable = cancelable;
	}

	@Override
	public void onCancelled() {
		failMsg();
		super.onCancelled();
	}

	/**
	 * 准备运行。该回调方法在任务被执行之后一般立即由主线程调用。这个步骤通常用来建立任务，例如在主线程上显示进度条。
	 */
	@Override
	public void onPreExecute() {

		String title = "";
		String message = mActivity.getString(mLoadingMsg);
		OnCancelListener cancelListener = new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialogInterface) {
				// AsyncTask.cancel()
				LoadingDialogTask.this.cancel(true);
			}
		};
		mProgressDialog = ProgressDialog.show(mActivity, title, message, true,
				this.cancelable, cancelListener);
		mProgressDialog.setCanceledOnTouchOutside(false);
		super.onPreExecute();
	}

	/**
	 * 正在后台运行，该回调方法由后台线程在onPreExecute()方法执行结束后立即调用。
	 */
	@Override
	public Entity doInBackground(Input... params) {

		Entity result = null;
		try {
			// 调用doTaskInBackground()方法，将结果返回给result
			result = doTaskInBackground(params);
			if (result == null) {
				// ((CommonActivity) mActivity).handleToast(mActivity
				// .getString(mFailMsg));
				System.out.println("result is null");
			}

		} catch (Exception e) {
			// ((CommonActivity) mActivity).handException(e, params);
			handException(e);
			System.out.println("result is null" + e);
		}
		return result;
	}

	/**
	 * 进度更新，该方法由主线程在publishProgress()方法调用完后被调用，例如可用于动态地显示一个进度条。
	 */
	@Override
	protected void onProgressUpdate(WSError... values) {

		Toast.makeText(mActivity, values[0].getMessage(), Toast.LENGTH_LONG)
				.show();
		this.cancel(true);
		mProgressDialog.dismiss();
		super.onProgressUpdate(values);
	}

	/**
	 * 完成后台任务，一般后台任务执行完之后被调用，在主线程执行。
	 */
	@Override
	public void onPostExecute(Entity result) {
		super.onPostExecute(result);
		
		mProgressDialog.dismiss();

		System.out.println("LoadingDialogTask -> ProgressDialog.dismiss()!");

		if (result != null) {
			doStuffWithResult(result);
		}
	}

	protected void handException(Exception e) {
	}

	protected void failMsg() {
		Toast.makeText(mActivity, mFailMsg, Toast.LENGTH_LONG).show();
	}

	public void doCancel() {
		mProgressDialog.dismiss();
	}

	public abstract Entity doTaskInBackground(Input... params);

	public abstract void doStuffWithResult(Entity result);

}
